﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TEL.ClientClasses;
using TEL.TelPhoneF.InfoList;

namespace TEL.TelPhoneF.Controls
{
    public partial class DataListCustom : UserControl
    {
        private object mLock = new object();
        public DataListCustom()
        {
            InitializeComponent();
            dataList.Paint += new PaintEventHandler(dataList_Paint);
            this.SetStyle(ControlStyles.DoubleBuffer,true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.UserPaint, true);
        }

        void dataList_Paint(object sender, PaintEventArgs e)
        {
            
        }

        private bool m_vid;
        public virtual bool Vid 
        { get { return m_vid; } 
            set { m_vid = value; } }

        public event EventHandler<StrErrorEventArgs> ErrorEvent;

        protected void OnErrorEvent(StrErrorEventArgs e)
        {
            if (ErrorEvent != null) ErrorEvent(this, e);
        }

        protected virtual void toolStripClear_Click(object sender, EventArgs e)
        {

        }

        protected virtual void ListItemAdd(ListViewItem item)
        {
            lock (mLock)
            {
                try
                {
                    this.Invoke(new MethodInvoker(delegate()
                                                      {
                                                          dataList.Items.Add(item);
                                                      }), null);
                }
                catch (Exception error)
                {
                    OnErrorEvent(new StrErrorEventArgs("DataListCustom ListItemAdd", error, false));
                }
            }
        }

        protected  void ReDraw()
        {
            lock (mLock)
            {
                try
                {
                    this.Invoke(new MethodInvoker(delegate()
                    {
                        dataList.Invalidate();
                    }), null);
                }
                catch (Exception error)
                {
                    OnErrorEvent(new StrErrorEventArgs("DataListCustom ListItemAdd", error, false));
                }
            }
            
        }
        
        public virtual void LoadList()
        {

        }

        protected virtual void SaveList()
        {

        }

        protected void BeginUpdate()
        {
            lock (mLock)
            {
                try
                {
                    this.Invoke(new MethodInvoker(delegate()
                    {
                        dataList.BeginUpdate();

                    }), null);
                }
                catch (Exception error)
                {
                    OnErrorEvent(new StrErrorEventArgs("DataListCustom ListItemAdd", error, false));
                }      
            }
          
        }

        protected void EndUpdate()
        {
            lock (mLock)
            {
                try
                {
                    this.Invoke(new MethodInvoker(delegate()
                                                      {
                                                          dataList.EndUpdate();

                                                      }), null);
                }
                catch (Exception error)
                {
                    OnErrorEvent(new StrErrorEventArgs("DataListCustom ListItemAdd", error, false));
                }
            }
        }

        protected void ClearList()
        {
            lock (mLock)
            {
                try
                {
                    this.Invoke(new MethodInvoker(delegate()
                                                      {
                                                          dataList.Items.Clear();

                                                      }), null);
                }
                catch (Exception error)
                {
                    OnErrorEvent(new StrErrorEventArgs("DataListCustom ListItemAdd", error, false));
                }
            }
        }

       private void dataList_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            float f = (float)8.25;
            Font font = new Font("Microsoft Sans Serif", f, FontStyle.Regular);
            Font fontB = new Font("Microsoft Sans Serif", f, FontStyle.Bold);
            Bitmap img = Properties.Resources.book;
            try
            {
                Contact contact = e.Item.Tag as Contact;
                if (contact != null && contact.Favorite)
                {
                    img = Properties.Resources.favorit;
                }
                else
                {
                    switch (e.Item.ImageIndex)
                    {
                        case 0:
                            img = Properties.Resources.user_blue;
                            break;
                        case 1:
                            img = Properties.Resources.user_green;
                            break;
                        case 2:
                            img = Properties.Resources.user_red;
                            break;
                        case 3:
                            img = Properties.Resources.book;
                            break;
                    }
                }
            }
            catch (Exception)
            {
            }
            if ( (e.State & ListViewItemStates.Focused) == ListViewItemStates.Focused)
            {
               
                e.Graphics.DrawImage(img, e.Bounds.Location);
                e.Graphics.DrawString(e.Item.Text, fontB, Brushes.Yellow, e.Bounds.Location + new Size(20, 0));
            }
            else
            {
                e.Graphics.DrawImage(img, e.Bounds.Location);
                e.Graphics.DrawString(e.Item.Text, font, Brushes.White, e.Bounds.Location + new Size(20,0));
            }

        }

        private void dataList_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            Brush b = new SolidBrush(Form1.BlueSkinColor);
            float f = (float) 8.25;
            Font font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
            e.Graphics.FillRectangle(Brushes.Black, e.Bounds);
            e.Graphics.DrawString(e.Header.Text, font, b, e.Bounds);
        }
        protected virtual void txtSearch_TextChanged(object sender, EventArgs e)
        {
        }

        protected virtual void dataList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
        }

        private void dataList_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            if(e.Header.DisplayIndex == 0) return;
            float f = (float)8.25;
            Font fontB = new Font("Microsoft Sans Serif", f, FontStyle.Bold);
            
            Font font = new Font("Microsoft Sans Serif", f, FontStyle.Regular);

            if ((e.ItemState & ListViewItemStates.Focused) == ListViewItemStates.Focused)
            {
                e.Graphics.DrawString(e.SubItem.Text, fontB, Brushes.Yellow, e.Bounds);
            }
            else
            {
                e.Graphics.DrawString(e.SubItem.Text, font, Brushes.White, e.Bounds);
            }

        }
    }
}
